# coding=utf8

from app import create_app
from app.extensions import db
from app.modules.common.utils import DbCommit
from app.modules.entrance.models import EntranceInfo
from app.modules.apps.models import AppInfo

application = create_app(flask_config = 'local')


def main(_env_name, _zone_code, _type, _hosts):
    with application.app_context():
        _entrance = EntranceInfo.query.filter_by(type = _type, env_name = _env_name, zone_code = _zone_code).first()
        for _host in _hosts:
            _info = AppInfo(app_name = _host['name'], host = _host['ip'], jmx_port = _host['jmx_port'],
                            app_type = "java", entrance_id = _entrance.id, entrance_type = _type,
                            current_status = "GNONE", expect_status = "GNONE", zone_code = _zone_code,
                            group = _host['group'], env_name = _env_name)
            print _info.to_dict()
            db.session.add(_info)
        DbCommit()


if __name__ == '__main__':
    _zone_code = 'uc.gzidc.area'
    _type = "jmx"

    _env_name = 'staging'
    _hosts = [
        {"name": "erp", "ip": "10.32.16.20", "jmx_port": "9999", "group": "A"},
        {"name": "erp", "ip": "10.32.16.21", "jmx_port": "9999", "group": "B"},
        {"name": "vms", "ip": "10.32.16.30", "jmx_port": "10033", "group": "A"},
        {"name": "vms", "ip": "10.32.16.21", "jmx_port": "10033", "group": "B"},
    ]

    # _env_name = 'online'
    # _hosts = [
    #     {"name": "erp", "ip": "10.32.0.17","jmx_port":"9999", "group": "A"},
    #     {"name": "erp", "ip": "10.32.0.27","jmx_port":"9999", "group": "B"},
    #     {"name": "erp", "ip": "10.32.0.37","jmx_port":"9999", "group": "B"},
    #     {"name": "erp", "ip": "10.32.0.18","jmx_port":"9999", "group": "A"},
    #     {"name": "erp", "ip": "10.32.0.28","jmx_port":"9999", "group": "A"},
    #     {"name": "erp", "ip": "10.32.0.38","jmx_port":"9999", "group": "B"},
    #     {"name": "erp", "ip": "10.32.0.77","jmx_port":"9999", "group": "B"},
    #     {"name": "erp", "ip": "10.32.0.24","jmx_port":"9999", "group": "A"},
    #     {"name": "erp", "ip": "10.32.0.44","jmx_port":"9999", "group": "B"},
    # ]


    main(_env_name, _zone_code, _type, _hosts)
    pass
